﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AlgoImpl.ConverterAnalyzer;

namespace AlgoImpl
{
    public class SecondStepManager : StepManagerAbstract
    {
        private Dictionary<string, List<string>> m_NewShiftRequestForStep;

        public SecondStepManager(ShiftRequests sr, int numOfIterations)
        {
            MRequests = sr;
            m_Converter = new Converter();
            m_AlgoExecuter = new AlgoExecuter();
            m_NumOfIterations = numOfIterations;

        }

        private void organizeRequestForSecondStep()
        {
            m_NewShiftRequestForStep = new Dictionary<string, List<string>>();
            foreach (string _worker in m_AnalyzerMgr.BestRound.NotArrangedWorkers)
            {
                List<string> notforbidenShifts = new List<string>();
                foreach (string _shift in m_AnalyzerMgr.BestRound.NotFullShifts)
                {
                    if (!MRequests.IsShiftForbidenForWorker(_worker, _shift) &&
                        !SharedData.Instance.GetWorkerById(_worker).IsRegisterdToShift(_shift))
                    {
                        notforbidenShifts.Add(_shift);
                    }
                }
                m_NewShiftRequestForStep.Add(_worker, notforbidenShifts);
            }
        }

        protected override void preperations()
        {
            organizeRequestForSecondStep();
        }

        protected override void finalize()
        {
            updateGlobalResultData(eStepThatGotShift.SECOND);
        }

        protected override void analyzeResults()
        {
            m_AnalyzerMgr.AnalyzeFirstStep(MRequests);
        }

        protected override void runAlgo()
        {
            m_AlgoExecuter.Execute();
        }

        protected override void convertToGraph()
        {
            m_Converter.ConvertToGraph(getAllShifts(), getAllWorkers(), m_NewShiftRequestForStep);
        }
    }
}
